package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Comment;
import com.gaohe.pojo.Room;
import com.gaohe.pojo.Shop;
import com.gaohe.service.CommentService;
import com.gaohe.service.ShopService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shop")
public class ShopController {
    @Autowired
    ShopService shopService;

    @Autowired
    RedisTemplate redisTemplate;

//    分页
    @GetMapping("/page")
    private R<Page> page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String name)
    {
        Page<Shop> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Shop::getShopName, name);
        lambdaQueryWrapper.orderByDesc(Shop::getShopName);
        lambdaQueryWrapper.orderByAsc(Shop::getUpdateTime);
        return R.success(shopService.page(page1,lambdaQueryWrapper));
    }

//    查询id
    @GetMapping("{id}")
    private R getId(@PathVariable Long id)
    {
        return R.success(shopService.getById(id));
    }

//    更新数据
    @PutMapping
    private R update(@RequestBody Shop shop)
    {
        boolean flag = shopService.updateById(shop);
        return flag?R.success("修改成功"):R.error("修改失败");
    }

//    查询在售商品列表
    @GetMapping("/getShopList")
    public R list(){
        LambdaQueryWrapper<Shop> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Shop::getStatus,1);
        List<Shop> list = shopService.list(lqw);
        return R.success(list);
    }

//    根据分类id查询商品列表
    @GetMapping("/getShopByCategoryId/{categoryId}")
    public R getShopByCategoryId(@PathVariable Long categoryId){
        LambdaQueryWrapper<Shop> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Shop::getCategoryId,categoryId);
        return R.success(shopService.list(lqw)) ;
    }

//    添加数据
    @PostMapping
    private R save(@RequestBody Shop shop, HttpSession httpSession)
    {
        shop.setCreateTime(LocalDateTime.now());
        shop.setUpdateTime(LocalDateTime.now());
        Long user = (Long)redisTemplate.opsForValue().get("auth");
//        Long user = (Long)httpSession.getAttribute("user");
        shop.setCreateUser(user);
        shop.setUpdateUser(user);
        boolean flag = shopService.save(shop);
        return flag?R.success("添加成功"):R.error("添加失败");
    }

//     删除商品和评论
    @Autowired
    CommentService commentService;

    @DeleteMapping
    private R deleted(Long id)
    {
        LambdaQueryWrapper<Comment>lwq = new LambdaQueryWrapper<>();
        lwq.eq(Comment::getShopId,id);
        commentService.remove(lwq);
        return shopService.removeById(id)?R.success("删除成功"):R.error("删除失败");
    }


}
